"""Test case for django_ldap.options"""


option_test = """
# django_ldap should not change the behavior of database models
>>> from django_ldap.tests.fixtures import db_model, ldap_model
>>> db_model._ldap_options
Traceback (most recent call last):
   ...
AttributeError: type object 'db_model' has no attribute '_ldap_options'

# django_ldap models should have option attribute
>>> ldap_model._ldap_options.__class__
<class 'django_ldap.options.Options'>

# django_ldap models should inherit ldap options from parent model
>>> ldap_model._ldap_options.objectClass
['inetOrgPerson']

# try get all atts for ldap_model models
>>> 'userCertificate' in ldap_model._ldap_options.attr.keys()
True

# ldap options in django_ldap models can be override by model settings
>>> ldap_model._ldap_options.rdn
['cn']

# ldap options in django_ldap models can be override by global settings
>>> from django.conf import settings
>>> setattr(settings, 'TEST_MODEL_SETTINGS', {'rdn':'uid'})
>>> from django_ldap.fields import ManyToManyField
>>> from django_ldap.models import GeneralModel
>>> class test_model(ldap_model):
...   class Ldap:
...     single_value_attr = ['test_attr']
...     dn_pattern = 'cn=%(cn)s'
...     attr_mapping = {'full_name': 'cn', 'username': 'uid'}
...     group_attr = {'group': {'to': 'django_ldap.models.GeneralModel', 'from_field': 'member'},
...                   'group2': ManyToManyField(GeneralModel, 'member')}
>>> test_model._ldap_options.rdn
['uid']
>>> 'test_attr' in test_model._ldap_options.single_value_attr
True
>>> test_obj = test_model(cn='test', uid='test')
>>> test_obj.get_default_id()[:7]
'cn=test'

# test attribute mapping
>>> test_obj._ldap_options.attr_mapping
{'username': 'uid', 'full_name': 'cn'}
>>> test_obj.full_name
'test'
>>> test_obj.username
'test'
>>> test_obj.username = 'test1'
>>> test_obj.uid
u'test1'
>>> test_model.objects.filter(cn='testuser1')[0].cn
u'testuser1'
>>> test_model.objects.filter(full_name='testuser1')[0].cn
u'testuser1'
>>> test_obj = test_model(username='test')
>>> test_obj.uid
'test'

# test group attribute
>>> test_obj = test_model.objects.filter(cn='testuser1')[0]
>>> test_obj.group[0].cn
u'testgroup'
>>> test_obj.group2[0].cn
u'testgroup'

# test declare a bad model
>>> class bad_model(ldap_model):
...   class Ldap:
...     group_attr = {'group': {'to': 'bad_module.BadModel', 'to_field': 'member'}}
>>> bad_model().group
Traceback (most recent call last):
   ...
FieldInitException: Error importing field class bad_module: "No module named bad_module"
"""

__test__ = {
    'option_test': option_test,
}